﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Common.Logging;
using Spring.Context;
using Spring.Context.Support;

namespace Dukou.Web
{
    public class SpringControllerFactory : DefaultControllerFactory, IGlobalModule
    {
        /// <summary>
        /// 日志器
        /// </summary>
        private static readonly ILog logger = LogManager.GetLogger(typeof(SpringControllerFactory));

        private IControllerFactory m_defaultControllerFactory;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="requestContext"></param>
        /// <param name="controllerName"></param>
        /// <returns></returns>
        public override IController CreateController(RequestContext requestContext, string controllerName)
        {
            try
            {
                //BOSSApplicationContext.Init(requestContext.HttpContext.ApplicationInstance.Context);
                IApplicationContext appContext = ContextRegistry.GetContext();
                string typeName = string.Format("{0}Controller", controllerName);
                IController controller = null;

                if (appContext.ContainsObject(typeName))
                {
                    controller = appContext.GetObject(typeName, typeof(IController)) as IController;
                }
                else
                {
                    if (this.m_defaultControllerFactory == null)
                    {
                        this.m_defaultControllerFactory = new DefaultControllerFactory();
                    }
                    controller = this.m_defaultControllerFactory.CreateController(requestContext, controllerName);
                }

                logger.Info(string.Format("CreateController: {0} - {1}", controllerName, controller));
                return controller;
            }
            catch (Exception ex)
            {
                logger.Error(string.Format("CreateController: {0} - FAILED", controllerName), ex);
                return null;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="controller"></param>
        public override void ReleaseController(IController controller)
        {
            try
            {
                IApplicationContext appContext = ContextRegistry.GetContext();
                if (appContext.ContainsObject(controller.GetType().Name))
                {
                    if (controller is IDisposable)
                    {
                        ((IDisposable)controller).Dispose();
                    }
                }
                else
                {
                    if (this.m_defaultControllerFactory == null)
                    {
                        this.m_defaultControllerFactory = new DefaultControllerFactory();
                    }
                    this.m_defaultControllerFactory.ReleaseController(controller);
                }
                logger.Info(string.Format("ReleaseController: {0}", controller));
            }
            catch (Exception ex)
            {
                logger.Debug(string.Format("ReleaseController: {0} - FAILED", controller), ex);
            }
        }

        public string Name
        {
            get { return "Spring 控制器工厂"; }
        }

        public void Initialize(HttpApplication application, IApplicationContext appContext)
        {
            ControllerBuilder.Current.SetControllerFactory(this);
        }
    }
}
